﻿function uiex($p) {
    
    //自动完成
    $(".autocomplete", $p).each(function () {
        var $this = $(this);
        var url = unescape($this.attr("autocompletehref")).replaceTmById($p);
        $this.autocomplete(url);
    });

    //2011-10-13添加获取物料最后一次采购价
    $(".GetOITMLastPurPrice", $p).blur(function () {
        var $this = $(this);
        var $showpriceid = $this.attr("showpriceid");
        var $itemcode = $this.val();
        var urlr = $(this).attr("autocompleteshowpricehref");
        $.ajax({
            type: 'get',
            url: urlr + '?ajaxmethod=GetOITMLastPurPrice&itemcode=' + $itemcode,
            // dataType: "text",
            async: true,
            cache: false,
            success: function (json) {
                try {
                    //alert(json);
                    var $showcardinfo = $("#" + $showpriceid, $p);
                    $showcardinfo.val(json);
                }
                catch (e) {

                }
            }
        });
    })

    /*自定义表单验证相关*/
    $("input.inputFloat", $p).inputFloat();
    $("input.inputGetFormLimtt", $p).inputGetFormLimtt();
    /*自定义Javascript代码*/
    ODailyAdd($p);
    //2.完成检查书2011.7.15
    $("input.OCInputBetween", $p).change(function () {
        var min = parseFloat($(this).attr("minx"));
        var max = parseFloat($(this).attr("maxx"));
        var $ngcontrol = $(this).closest("tr").find("select[name='NG_" + $(this).attr("name") + "']"); //$("#ok_" + $(this).attr("name") + "", $p);
        // alert($ngcontrol);
        var $val = $(this).val();
        if ($val.indexOf('->') > 0) {
            $ngcontrol.attr("value", "NG->OK");
            return;
        }
        var cValue = parseFloat($(this).val());
        try {
            if (cValue >= min && cValue <= max) {
                $ngcontrol.attr("value", "OK");
            }
            else {
                $ngcontrol.attr("value", "NG");
            }
        }
        catch (e) {
            $ngcontrol.attr("value", "NG->OK");
        }
    })
    //2011.11.22完成检查书编辑页
    $("table.octable", $p).find(":text").each(function () {
        $sel = $(this).closest("td").next("td:first").find("select");
        $(this).change();
    })
    //完检单
    OInspectionAdd();
    //2011.11.22完成检查书NG项自动加红色
    $("table.octable", $p).find("td").each(function () {
        if ($.trim($(this).html()) == "NG")
            $(this).css("color", "red");
    })
    //2011-8-23警报列表处理
    $("div.AlarmList", $p).find("a.click").each(function () {
        $(this).click(function () {
            $(this).closest("tr").css("color", "red");
            $.pdialog.minimize($.pdialog.getCurrent());
            return false;
        })
    })
    //2011-8-23来电回访添加客户资料自动添加
    $("#U4008RecordAddSerialNumber", $p).blur(function () {
        if ($(this).val() == "")
            return;
        $.ajax({
            type: 'get',
            url: '/MachineSys/sysAjaxDone.ashx?ajaxmethod=getUWarrantyCardBySerialNumbert&SerialNumbert=' + $(this).val(),
            dataType: "json",
            async: true,
            cache: false,
            success: function (json) {
                try {
                    if (json.CustomerName) {
                        $("#U4008RecordAddcustomername", $p).val(json.CustomerName);
                    }
                    if (json.IntoDate) {
                        $("#U4008RecordAddIntoDate", $p).val(json.IntoDate);
                    }
                    if (json.CustomerTel) {
                        $("#U4008RecordAddCustomerTel", $p).val(json.CustomerTel);
                    }
                    if (json.AgentName) {
                        $("#U4008RecordAddAgentName", $p).val(json.AgentName);
                    }
                }
                catch (e) {

                }
            }
        });
    })

    //2011-8-24短信模板标签
    $("table.smstempletetag", $p).find("td.smskey").click(function () {
        var $textare = $(this).closest("dd").find("textarea");
        $textare.insertAtCaret($(this).html())
    })

    //2011-8-25机器点检完成调查表
    $("div.UTallySurveyAdd", $p).find(":text[name='SerialNumber']").blur(function () {
        if ($(this).val() == "")
            return;
        $.ajax({
            type: 'get',
            url: '/MachineSys/sysAjaxDone.ashx?ajaxmethod=getUTallySurvey&SerialNumbert=' + $(this).val(),
            dataType: "json",
            async: true,
            cache: false,
            success: function (json) {
                try {
                    var $box2 = navTab.getCurrentPanel()
                    $box2.find("#MachineType").val(json.MachineType);
                    $box2.find("#WorkAddress").val(json.WorkAddress);
                    $box2.find("#WorkType").val(json.WorkType);
                    $box2.find("#WorkTime").val(json.WorkTime);
                    $box2.find("#AgentName").val(json.AgentName);
                    $box2.find("#AgentSvrPer").val(json.AgentSvrPer);
                    $box2.find("#AgentSvrPerTel").val(json.AgentSvrPerTel);
                }
                catch (e) {

                }
            }
        });
    })

    //2011-8-30BOM查询中关于供应商联系信息
    $("div.BOMSeacher", $p).find("tr.CardName").click(function () {
        var $cardcode = $(this).attr("cardcode");

        $.ajax({
            type: 'get',
            url: '/MachineSys/PublicModel/BOMSeacher.aspx?ajaxmethod=getcardinfo&cardcode=' + $cardcode,
            dataType: "json",
            async: true,
            cache: false,
            success: function (json) {
                try {
                    var $showcardinfo = $("div.showcardinfo", navTab.getCurrentPanel());
                    $showcardinfo.html(json.msg);
                }
                catch (e) {

                }
            }
        });
    })

    //对已列出的物料清单进行再一次查找
    $("div.BOMSeacher", $p).find("button.seacherItemName").click(function () {
        var index = 1;
        var param = $("[name='ItemName']", navTab.getCurrentPanel()).val();
        $('div.gridTbody', navTab.getCurrentPanel()).find('tr').each(function () {
            $(this).show();
            var targetText = $($(this).find('td').get(0)).text();
            var targetText2 = $($(this).find('td').get(1)).text();
            if (targetText.indexOf(param) == -1 && targetText2.indexOf(param) == -1) {
                $(this).hide();
            }
        });
    });
    //2011-09-3
    $("div.OProjectAddForm", $p).find("[name='MachineType']").change(function () {
        var $PName = $(this).closest("div").find("[name='PName']");
        var $TypeSuff = $(this).closest("div").find(".TypeSuff");
        var $macsuff = $(this).attr("macsuff");
        $PName.val($(this).val());
        $TypeSuff.val($macsuff);
    });
    //2011-12-14 作业项目定义机号处理
    $("div.OProjectAddForm", $p).find(".StartNum,.EndNum").blur(function () {
        var $oldValue = String( $(this).val());
        if ($oldValue.Length > 6) {
            alert("对不起,机号不能大于6位数");
            $(this).focus();
            return
        }
        var $suff = '';
        var $laststr = $oldValue;
        var $newValue = $laststr;
        if ($laststr.length < 6) {
            var $oldlen = $laststr.length;
            for (var i = $oldlen + 1; i <= 6; i++) {
                $newValue = "0" + $newValue;
            }
        }
        $newValue = $suff + $newValue
        $(this).val($newValue)
    })
    //2011-12-14
    $("div.OProjectAddForm", $p).find(".MacNum").blur(function () {
        var $macnum = $(this).val();
        var $StartNum = parseFloat(String($("div.OProjectAddForm", $p).find(".StartNum").val()));
        var $MACSERLen = 1
        var $EndNum = $StartNum
        var $snum =1
        while ($snum < $macnum) {
            $EndNum = parseInt($EndNum) + 1
            if (String($EndNum).substring(String($EndNum).length - 1, String($EndNum).length) != '4') {
                $snum = $snum + 1
            }
        }
        $("div.OProjectAddForm", $p).find(".EndNum").val($EndNum)
        $("div.OProjectAddForm", $p).find(".EndNum").blur()
    })
    //2011-9-29机号自动补齐
    $(".SerialNumber", $p).blur(function () {
        var $oldValue = $(this).val();
        if ($oldValue.length <= 2)
            return;
        var $suff = $oldValue.substr(0, 2);
        var $laststr = $oldValue.substr(2);
        var $newValue = $laststr;
        if ($laststr.length < 6) {
            var $oldlen = $laststr.length;
            for (var i = $oldlen + 1; i <= 6; i++) {
                $newValue = "0" + $newValue;
            }
        }
        $newValue = $suff + $newValue
        $(this).val($newValue)
    })
    /*初加工物料管制表2011-10-15*/
    $(".btnShowAllSerialNumber", $p).toggle(function () {

        $(".SerialNumberShow:visible").each(function () {
            $(this).slideToggle(); $(this).closest("div").removeClass("SerialNumberShowBorder");
            $(this).closest("div").find(".btnShowAllSerialNumber").removeClass("btnShowAllSerialNumberexpandable");
            $(this).closest("div").find(".btnShowAllSerialNumber").addClass("btnShowAllSerialNumbercollapsable");
        })


        $this = $(this);
        $SerialNumberShow = $this.closest("div").find(".SerialNumberShow");


        $SerialNumberShow.slideToggle();
        $this.removeClass("btnShowAllSerialNumbercollapsable");
        $this.addClass("btnShowAllSerialNumberexpandable");
        var $div = $this.closest("div");
        var $left = $div.position().left;
        var $top = $div.position().top + $div.outerHeight() - 1;
        var $width = $div.outerWidth() - 0;
        $SerialNumberShow.css("left", $left).css("top", $top).width($width);
        $this.closest("div").addClass("SerialNumberShowBorder");

    },
    function () {
        $(".SerialNumberShow:visible").each(function () {
            $(this).slideToggle();
            $(this).closest("div").removeClass("SerialNumberShowBorder");
            $(this).closest("div").find(".btnShowAllSerialNumber").removeClass("btnShowAllSerialNumberexpandable");
            $(this).closest("div").find(".btnShowAllSerialNumber").addClass("btnShowAllSerialNumbercollapsable");
        })

    }

    )

    /*2011-10-24更新用户资料系统代理商列表显示*/
    $(".openselectagent", $p).click(function () {
        var $agentlistdiv = $(this).closest(".pageContent").find("div.agentlistdiv");
        $agentlistdiv.show().find("a.closeselectagent").click(function () { $agentlistdiv.hide(); });
        //列表高度
        var iContentH_UAMSL = $(this).closest(".pageContent").height() - $agentlistdiv.find(".panelBar").height() - 5;
        $agentlistdiv.find(".agentlisttreecon").height(iContentH_UAMSL);
    })
    /*2011-10-24个性化设置*/
    $(".UserMySetting", $p).find("input,select").change(function () {
        var $UserMySetting = $(this).closest(".UserMySetting");

        $.ajax({
            type: 'get',
            url: '/MachineSys/sysAjaxDone.ashx?ajaxmethod=changemysetting',
            data: $UserMySetting.serialize2(),
            dataType: "json",
            async: false,
            cache: false,
            success: function (json) {
                
            }
        });
    })

    /*2011-10-27索赔管控-->更新供应商索赔明细*/
    $(".UClaimDemageControlUpdateSupplerDetail", $p).find("button.submit").click(function () {
        var $SupplerDetailDIV = $(this).closest("form").find("tr.supplerdetailitem");
        $SupplerDetailDIV.each(function () {  
            $.ajax({
                type: 'post',
                url: '/MachineSys/sysAjaxDone.ashx?ajaxmethod=uclaimdemagecontrolupdatesupplerdetail',
                data: $(this).serialize2(),
                async: false,
                cache: false,
                success: function (json) {
                    if (json == "") {
                        setTimeout(function () {
                            alertMsg.correct('已成功保存您的数据！');
                             $.pdialog.closeCurrent();
                           // navTab.closeCurrentTab()
                        }, 2000);


                    }
                    else
                        alertMsg.error('保存您的数据失败！' + json);
                }
            });
        });

       

    })
    /*2011-10-28索赔管控-->更新索赔结算明细*/
    $(".UClaimDemageControlUpdateFinalDetail", $p).find("button.submit").click(function () {
        var $SupplerDetailDIV = $(this).closest("form").find("tr.finaldetailitem");
        $SupplerDetailDIV.each(function () {
            $.ajax({
                type: 'post',
                url: '/MachineSys/sysAjaxDone.ashx?ajaxmethod=uclaimdemagecontrolupdatefinaldetail',
                data: $(this).serialize2(),
                async: false,
                cache: false,
                success: function (json) {
                    if (json == "") {
                        setTimeout(function () {
                            alertMsg.correct('已成功保存您的数据！');
                            $.pdialog.closeCurrent();
                           // navTab.closeCurrentTab()
                        }, 2000);
                       
                       
                    }
                    else
                        alertMsg.error('保存您的数据失败！' + json);
                }
            });
            
        })

    })

    //2012-4-10 生产目标
    $("#OJobGoalform1_edit", $p).click(function () {
        var $form = $(this).closest("form");
        $.ajax({
            type: 'post',
            url: 'MacModel/OJobGoal.aspx?ajaxmethod=edit',
            data: $form.serialize2(),
            dataType: "json",
            async: false,
            cache: false,
            success: function (json) {
                //
                navTabAjaxDone(json);
            }
        });
    })


    //2012-5-21 机号与发动机号一致对比
    $(".checkEngineNumber", $p).blur(function () {
        var $EngineNumber = $(this).val();
        var $SerialNumber = $p.find("input[name='SerialNumber']").val();
        var $vtype = $(this).attr("vtype");

        $.ajax({
            type: 'post',
            url: '/MachineSys/sysAjaxDone.ashx?ajaxmethod=checkEngineNumber&EngineNumber=' + $EngineNumber + '&SerialNumber=' + $SerialNumber + '&vtype=' + $vtype,
            async: false,
            cache: false,
            success: function (json) {
                if (json != "") {
                    alertMsg.error(json+"<b>请先检查相应的表单再录入.</b>");
                }
            }
        });
    })
}

/*新增作业日报表*/
function ODailyAdd() {
    var $box = navTab.getCurrentPanel();
    var $table = $box.find("tbody.ODailyAdd");
    $thead = $box.find("thead.ODailyAdd");
    $thead.find("#sameworkdate").click(function () {
        $firstworkdate = $table.find("input[name='WorkDate']:first").val();
        $table.find("input[name='WorkDate']").each(function () {
            if ($(this).attr("disabled") == false)
                $(this).val($firstworkdate);
        })
        // alert($firstworkdate)
    });
    $thead.find("#sameserialnumber").click(function () {
        $firstworkdate = $table.find("input[name='SerialNumber']:first").val();
        $table.find("input[name='SerialNumber']").each(function () {
            if ($(this).attr("disabled") == false)
                $(this).val($firstworkdate);
        })
        // alert($firstworkdate)
    })

   
}

/*新增完检单
2011-8-1
*/
function OInspectionAdd() {
    var $box = navTab.getCurrentPanel();
    var $table = $box.find("a.addDJNGItem").closest("table");
    var $addDJNGItem = $box.find("a.addDJNGItem");
    var $deleteDJNGItem = $box.find("a.deleteDJNGItem");
    $addDJNGItem.click(function () {
        OInspectionAddaddDJNGItem(this);
    });
    $deleteDJNGItem.click(function () {
        OInspectiondeleteDJNGItem(this);
    });
    $DJNGItemCode = $box.find(".DJNGExceptionrecord");
    $DJNGItemCode.blur(function () {
        if ($(this).val() != "" && ($(this).attr("addtag") == false || $(this).attr("addtag") == undefined)) {
            OInspectionAddaddDJNGItem(this);
            var $currtr = $(this).closest("tr");
            //ajax获取说明
            //            $.ajax({
            //                type: 'get',
            //                url: '/MachineSys/sysAjaxDone.ashx?ajaxmethod=getOTallyproject&itemcode=' + $(this).val(),
            //                dataType: "json",
            //                async: false,
            //                cache: false,
            //                success: function (json) {
            //                    try {
            //                        var $itemname = $currtr.find(".ItemName");
            //                        var $Standard = $currtr.find(".Standard");
            //                        //                    alert($itemname.html());
            //                        //                    alert(json.ItemName);
            //                        if (json.ItemName) {
            //                            $itemname.html(json.ItemName);
            //                        }
            //                        if (json.Standard) {
            //                            $Standard.html(json.Standard);
            //                        }
            //                    }
            //                    catch (e)
            //                    { }
            //                }
            //            });
        }
    })
    $box.find(".DJNGItemCode,.DJNGExceptionrecord").inputGetFormLimtt();
}
function OInspectionAddaddDJNGItem(jObj) {
    var $box = navTab.getCurrentPanel();
    var $table = $(jObj).closest("table");
    var $trItem = $table.find("tr:last");
    var $trItemCopy = $trItem.clone(false);
    //自动完成
//    if ($.fn.suggest)
//        $("input[suggestFields]", $trItemCopy).suggest();

    //自动完成
//    $(".autocomplete", $trItemCopy).each(function () {
//        var $this = $(this);
//        var url = unescape($this.attr("autocompletehref")).replaceTmById($trItemCopy);
//        $this.autocomplete(url);
//    });
    //
    $trItem.find(".DJNGExceptionrecord").attr("addtag", true);
    $trItem.find("a.addDJNGItem").hide();
    $trItem.find("a.deleteDJNGItem").show();
    $trItemCopy.find("a.deleteDJNGItem").hide();
    $trItemCopy.find(":text").val("");
    $table.append($trItemCopy);

    //自动完成
    $(".autocomplete", $table).each(function () {
            var $this = $(this);
            var url = unescape($this.attr("autocompletehref"));
            $this.autocomplete(url);
        });
        OInspectionAdd();
    //initUI($trItemCopy);
}
function OInspectiondeleteDJNGItem(jObj) {
    var $box = navTab.getCurrentPanel();
    var $table = $(jObj).closest("table");
    var $trItem = $(jObj).closest("tr");

    $trItem.remove();
}



/*表单验证*/
/*����
日报表输入表单验证
*/
function validateCallbackODailyAdd(form, callback) {

    var $form = $(form);
    var $table = $form.find("tbody.ODailyAdd");
    $table.find("input[name='WorkDate']").each(function () {
        if ($(this).val() == "") {
            $(this).closest("tr").find(":input").attr("disabled", "true"); ;
        }
    });
    //    $table.find("input[name='GongWei']").each(function () {
    //        if ($(this).val() == "") {
    //            $(this).closest("tr").find(":input").attr("disabled", "true"); ;
    //        }
    //    })
    $table.find("input[name='JobCode']").each(function () {
        if ($(this).val() == "") {
            $(this).closest("tr").find(":input").attr("disabled", "true"); ;
        }
    })
    //    $table.find("input[name='SerialNumber']").each(function () {
    //        if ($(this).val() == "") {
    //            $(this).closest("tr").find(":input").attr("disabled", "true"); ;
    //        }
    //    })
    $table.find("input[name='TaskTime']").each(function () {
        if ($(this).val() == "") {
            $(this).closest("tr").find(":input").attr("disabled", "true"); ;
        }
    })
    //return false;
    return validateCallback(form, callback);

}

function validateCallbackSendWPrdctShipLst(form, callback) {
    var $form = $(form);
    //var $table = $form.find("tbody.ODailyAdd");
    var errormsg = "";

    if ($form.find("input[name='ItemCode']").val() == "") {
        if ($form.find("select[name='PID']").val() == "0") {
            errormsg = "请选择需配料的作业项目<br/>";
        }
        if ($form.find("input[name='AliasName']").val() == "") {
            errormsg += "请选择一个供应商<br/>";
        }
       
    } else {
        
    }

    if (errormsg != "") {
        alertMsg.confirm(errormsg, {});
        return false;
    }

    return navTabSearch(form);
}

/*序列化指定内容*/
$.fn.serialize2 = function (type) {
    var _type = type == undefined ? true : false;

    var s = $('input, select, textarea', this).filter(function () {
        return this.name && !this.disabled &&
				(this.checked || /select|textarea/i.test(this.nodeName) ||
					/text|hidden|password/i.test(this.type));
    })
		.map(function (i, elem) {
		    var val = jQuery(this).val();
		    return val == null ? null :
				val.constructor == Array ?
					jQuery.map(val, function (val, i) {
					    return { name: elem.name, value: val };
					}) :
					{ name: elem.name, value: val };
		}).get();

    return _type ? s : jQuery.param(s);
};

